import * as React from 'react'
import RouterComponent from "./routers";
import ModalArray from "./models/index";
import Request from "./utils/Request";
import api from "./utils/Api";
import RouterServer from "./utils/RouterServer";
import {BasePage} from "./interface/router";


const {Actions} = require('react-native-router-flux');
const {create} = require('dva-core');
const {autoRehydrate} = require('redux-persist');
const {Provider} = require('react-redux');
const {debug} = require('../app.json');

Request.init(api, debug, 30 * 1000, {
    'invalid_token': (error) => {
        if (Actions.currentScene !== 'auth') {
            RouterServer.replace('auth');
        }

    },
    'Unauthorized': (error) => {
        if (Actions.currentScene !== 'auth') {
            RouterServer.replace('auth');
        }
    }
});

const app = create({
    extraEnhancers: [autoRehydrate()],
    onError: (e) => {
        // console.log('app have catch a error:', e);
    },
});

const initModels = (models = []) => {
    models.map((mode) => {
        app.model(mode);
    })
};

initModels(ModalArray);
app.start();
export default class App extends React.Component<BasePage, any> {
    constructor(props: any, context: any) {
        super(props, context);
    }


    shouldComponentUpdate(nextProps: BasePage, nextState: any, nextContext: any): boolean {
        console.log('app can update',this.props,nextProps);
        return null;
    }

    render() {
        return (
            <Provider store={app._store}>
                <RouterComponent/>
            </Provider>
        )
    }
}